<script setup>
import { Plus } from "@element-plus/icons-vue";
import useTable from "@/utils/useTable";
import {http} from "@/api";
import router from "@/router";
import {ref} from "vue";
const roleList = ref([])
http.role.list().then(res => {
  roleList.value = res.data
})
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openEdit,
  openDelete
} = useTable({
  format: (res) => {
    return res.map(el => {
      return {
        ...el,
        role_name: roleList.value.find(role => role.id === el.role_id)?.name
      }
    })
  },
  requestFn: http.user,
  routerPre: 'user',
  params: {
    name: '',
  }
})
</script>


<template>
  <PageMain title="用户管理">
    <el-form label-suffix="：" inline>
      <el-form-item label="账号">
        <el-input v-model="searchParams.username" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="add-box">
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="账号" prop="username">
        </el-table-column>
        <el-table-column label="类型">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-tag :type="scope.row.disabled === 0 ? 'primary' : 'info'">{{ scope.row.disabled === 0 ? '超级管理员' : '普通用户' }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="角色" prop="role_name">
        </el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-tag :type="scope.row.status === 0 ? 'success' : 'error'">{{ scope.row.status === 0 ? '启用' : '禁用' }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope" >
            <el-button v-if="scope.row.disabled" type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button v-if="scope.row.disabled" type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
